
$(function(){
    loadType();
    loadRecommended();
    loadRandom();
    loadTags();
    $("#one").click(function(){
        tagList=getChooseTags();
        if(tagList.length==0){
            toastr.error("请至少选择一个查询标签！");
            return;
        }
        var condition='';
        for(var i in tagList){
            condition+="tagIds="+tagList[i]+"&";
        }
        window.location.href="/list.html?"+condition;
    });
    $("#all").click(function(){
        tagList=getChooseTags();
        if(tagList.length==0){
            toastr.error("请至少选择一个查询标签！");
            return;
        }
        var condition='';
        for(var i in tagList){
            condition+="tagIds="+tagList[i]+"&";
        }
        window.location.href=encodeURI("/list.html?includeAllTags=true&"+condition);
    });

    $("#search button").click(function(){
        var keywords=$("#search input[name='keywords']").val();
        window.location.href="/list.html?keywords="+keywords;
    });
    $("body").append('<div id="toTop" class="hidden"><img src="/img/toTop.png" style="width: 100px;position:  fixed;bottom: 80px;right: 20px;"></div>');
    $("#toTop").click(function(){
        $("html,body").animate({scrollTop:0},500)
    });
});



var loadTagFlag=false,loadTypeFlag=false,loadRecommendedFlag=false,loadRandomFlag=false;//异步资源加载情况
//异步资源加载是否完毕
function isAllAajxLoadOver(){
    if(loadTagFlag&&loadTypeFlag&&loadRecommendedFlag&&loadRandomFlag){
        allResourcesLoadAction();
    }
}
//异步资源加载完毕调用方法
function allResourcesLoadAction(){
    //初始化滚动到顶部时固定的div事件
    var scrollTopFixDivH=$(".scrollTopFix").offset().top;
    var width=$(".scrollTopFix").outerWidth();
    $(document).scroll(function(){
        var scrollH=$(this).scrollTop();
        if(scrollH>700){
            $("#toTop").removeClass("hidden");
        }else{
            $("#toTop").addClass("hidden");
        }
        if(scrollH>scrollTopFixDivH-50){
            $(".scrollTopFix").css({"position": "fixed","top":"40px","width":width+"px"});
        }else{
            $(".scrollTopFix").css({"position":"relative","top":"0"});
        }
    });
}
function getChooseTags(){
    var chooseList=[];
    var i=0;
    $(".choose").each(function(){
        chooseList[i++]=$(this).attr("data-id");
    });
    return chooseList;
}

function loadTags(){
    $.ajax({
        url:'/api/tags/listAll',
        type:'post',
        success:function(resp){
            if(resp.status==200){
                var tagsHtml='';
                var tags=resp.data;
                for(var i in tags){
                    var tag=tags[i];
                    tagsHtml+='<div data-id="'+tag.id+'" title="'+tag.description+'" class="animated"><i class="fa fa-check-square hidden"></i>'+tag.name+'</div>';
                }
                $(".tag").html(tagsHtml);
                $(".tag div").click(function(){
                    $(this).toggleClass("choose");
                    $(this).children("i").toggleClass("hidden");
                });
                loadTagFlag=true;
                isAllAajxLoadOver();
            }else{
                toastr.error("加载标签失败！");
            }
        }
    });
}

function loadType(){
    $.ajax({
        url:'/api/type/listAll',
        type:'post',
        success:function(resp){
            if(resp.status==200){
                var types=resp.data;
                var typeHtml='';
                for(var i in types){
                    var type=types[i];
                    typeHtml+='<a href="/list.html?typeId='+type.id+'" title="'+type.description+'" class="animated">'+type.name+'</a>';
                }
                $(".type").html(typeHtml);
                loadTypeFlag=true;
                isAllAajxLoadOver();
            }else{
                toastr.error("分类加载失败！");
            }
        }
    });
}

function loadRecommended(){
    $.ajax({
        url:'/api/article/recommended',
        type:'post',
        data:{
            number:5
        },
        success:function(resp){
            if(resp.status==200){
                var recommendedHtml='';
                var recommendeds=resp.data;
                for(var i in recommendeds){
                    var rec=recommendeds[i];
                    recommendedHtml+='<li>\n' +
                        '\t\t\t\t\t\t<i><img style="height:100%;" src="'+rec.coverImage+'"></i><b><a href="/article/'+rec.id+'.html">'+rec.title+'</a></b>' +rec.description+
                        '\t\t\t\t\t</li>';
                }
                $(".recommendedArticles").html(recommendedHtml);
                loadRecommendedFlag=true;
                isAllAajxLoadOver();
            }else{
                toastr.error("推荐文章加载失败！");
            }
        }
    });
}

function loadRandom(){
    $.ajax({
        url:'/api/article/random',
        type:'post',
        data:{
            number:5
        },
        success:function(resp){
            if(resp.status==200){
                var randomHtml='';
                var randoms=resp.data;
                for(var i in randoms){
                    var random=randoms[i];
                    randomHtml+='<li><a href="/article/'+random.id+'.html">'+random.title+'</a>\n' +
                        '\t\t\t\t\t</li>';
                }
                $(".randomArticles").html(randomHtml);
                loadRandomFlag=true;
                isAllAajxLoadOver();
            }else{
                toastr.error("推荐文章加载失败！");
            }
        }
    });
}

function showAllCommentCount(){
    $(".comment_count").each(function(){
        var articleId=$(this).attr("data-id");
        $.ajax({
            url:'http://changyan.sohu.com/api/2/topic/load?client_id=cytF3GqGY&topic_url=1&topic_source_id='+articleId,
            dataType:'jsonp',
            async:false,
            success:function(resp){
                var count=resp.cmt_sum;
                var id=articleId;
                $(".comment_count[data-id='"+id+"']").html(count);
            }
        });
    });
}